package com.sh1604.duitang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sh1604.duitang.R;
import com.sh1604.duitang.mybean.Bean;
import com.squareup.picasso.Picasso;

import java.util.List;

public class HomePagerAdapter extends BaseAdapter {
    private Context mContext;
    private List<Bean.DataBean.ObjectListBean> mList;


    public HomePagerAdapter(Context mContext, List<Bean.DataBean.ObjectListBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }
    private final int TYPE_PGC_LARGE = 0;
    private final int TYPE_PGC_SMALL = 1;
    private final int TYPE_STORE = 2;
    private final int TYPE_ALBUM_COLLECT = 3;
    private final int TYPE_TOPIC_JOIN = 4;
    private final int count = TYPE_TOPIC_JOIN+1;

    @Override
    public int getItemViewType(int position) {
        String content_type = mList.get(position).getContent_type();
        String style = mList.get(position).getStyle();
        if ("pgc".equals(content_type)){
            if ("large".equals(style)){
                return TYPE_PGC_LARGE;
            }
            return TYPE_PGC_SMALL;
        }else if ("store".equals(TYPE_STORE)){
            return TYPE_STORE;
        }else if ("album".equals(TYPE_ALBUM_COLLECT)){
            return TYPE_ALBUM_COLLECT;
        }
        return TYPE_TOPIC_JOIN;
    }

    @Override
    public int getViewTypeCount() {
        return count;
    }

    class LargerHolder{
        private ImageView iv_larger;
        private TextView tv_larger_top,tv_larger_bottom;

        public LargerHolder(View view) {
           iv_larger = (ImageView) view.findViewById(R.id.iv_larger);
            tv_larger_top = (TextView) view.findViewById(R.id.tv_larger_top);
            tv_larger_bottom= (TextView) view.findViewById(R.id.tv_larger_bottom);
        }
    }
    class SmallHolder{
        private ImageView iv_small;
        private TextView tv_small_top,tv_small_centre,tv_small_bottom;

        public SmallHolder(View view1) {
            iv_small = (ImageView) view1.findViewById(R.id.iv_small);
            tv_small_top = (TextView) view1.findViewById(R.id.tv_small_top);
            tv_small_centre = (TextView) view1.findViewById(R.id.tv_small_centre);
            tv_small_bottom = (TextView) view1.findViewById(R.id.tv_small_bottom);
        }
    }
    class StoreHolder{
        private ImageView iv_store;
        private TextView tv_store_top,tv_store_centre,tv_store_bottom;

        public StoreHolder(View view1) {
            iv_store = (ImageView) view1.findViewById(R.id.iv_store);
            tv_store_top = (TextView) view1.findViewById(R.id.tv_store_top);
            tv_store_centre = (TextView) view1.findViewById(R.id.tv_store_centre);
            tv_store_bottom = (TextView) view1.findViewById(R.id.tv_store_bottom);
        }
    }
    class CollectHolder{
        private ImageView iv_collect;
        private TextView tv_collect_top,tv_collect_centre,tv_collect_bottom;

        public CollectHolder(View view1) {
            iv_collect = (ImageView) view1.findViewById(R.id.iv_collect);
            tv_collect_top = (TextView) view1.findViewById(R.id.tv_collect_top);
            tv_collect_centre = (TextView) view1.findViewById(R.id.tv_collect_centre);
            tv_collect_bottom = (TextView) view1.findViewById(R.id.tv_collect_bottom);
        }
    }
    class JoinHolder{
        private ImageView iv_join;
        private TextView tv_join_top,tv_join_bottom;

        public JoinHolder(View view4) {
            iv_join = (ImageView) view4.findViewById(R.id.iv_join);
            tv_join_top = (TextView) view4.findViewById(R.id.tv_join_top);
            tv_join_bottom = (TextView) view4.findViewById(R.id.tv_join_bottom);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemType = getItemViewType(position);
        LargerHolder largerHolder = null;
        SmallHolder smallHolder = null;
        StoreHolder storeHolder = null;
        CollectHolder collectHolder = null;
        JoinHolder joinHolder = null;
        if (convertView == null){
            switch (itemType){
                case TYPE_PGC_LARGE:
                    convertView= LayoutInflater.from(mContext).inflate(R.layout.item_larger, parent, false);
                    largerHolder = new LargerHolder(convertView);
                    convertView.setTag(largerHolder);
                    break;
                case TYPE_PGC_SMALL:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_small, parent, false);
                    smallHolder = new SmallHolder(convertView);
                    convertView.setTag(smallHolder);
                    break;
                case TYPE_STORE:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_store, parent, false);
                    storeHolder = new StoreHolder(convertView);
                    convertView.setTag(storeHolder);
                    break;
                case TYPE_ALBUM_COLLECT:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_collect, parent, false);
                    collectHolder = new CollectHolder(convertView);
                    convertView.setTag(collectHolder);
                    break;
                case TYPE_TOPIC_JOIN:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_join,parent,false);
                    joinHolder = new JoinHolder(convertView);
                    convertView.setTag(joinHolder);
                    break;
            }
        }else{
            switch (itemType){
                case TYPE_PGC_LARGE :
                    largerHolder = (LargerHolder) convertView.getTag();
                    break;
                case TYPE_PGC_SMALL:
                    smallHolder = (SmallHolder) convertView.getTag();
                    break;
                case TYPE_STORE:
                    storeHolder = (StoreHolder) convertView.getTag();
                    break;
                case TYPE_ALBUM_COLLECT:
                    collectHolder = (CollectHolder) convertView.getTag();
                    break;
                case TYPE_TOPIC_JOIN:
                    joinHolder = (JoinHolder) convertView.getTag();
                    break;
            }
        }
        Bean.DataBean.ObjectListBean bean = mList.get(position);
        switch (itemType){
            case TYPE_PGC_LARGE:
                Picasso.with(mContext).load(bean.getImage_url()).resize(200,200).into(largerHolder.iv_larger);
                largerHolder.tv_larger_top.setText(bean.getDescription());
                largerHolder.tv_larger_bottom.setText(bean.getStitle());
                break;
            case TYPE_PGC_SMALL:
                Picasso.with(mContext).load(bean.getImage_url()).into(smallHolder.iv_small);
                smallHolder.tv_small_top.setText(bean.getDescription());
                smallHolder.tv_small_centre.setText(bean.getStitle());
                smallHolder.tv_small_bottom.setText(bean.getDynamic_info());
                break;
            case TYPE_STORE:
                Picasso.with(mContext).load(bean.getImage_url()).into(storeHolder.iv_store);
                storeHolder.tv_store_top.setText(bean.getDescription());
                storeHolder.tv_store_centre.setText(bean.getStitle());
                storeHolder.tv_store_bottom.setText(bean.getDynamic_info());
                break;
            case TYPE_ALBUM_COLLECT:
                Picasso.with(mContext).load(bean.getImage_url()).into(collectHolder.iv_collect);
                collectHolder.tv_collect_top.setText(bean.getDescription());
                collectHolder.tv_collect_centre.setText(bean.getStitle());
                collectHolder.tv_collect_bottom.setText(bean.getDynamic_info());
                break;
            case TYPE_TOPIC_JOIN:
                Picasso.with(mContext).load(bean.getImage_url()).into(joinHolder.iv_join);
                joinHolder.tv_join_top.setText(bean.getDescription());
                joinHolder.tv_join_bottom.setText(bean.getDynamic_info());
                break;
        }

        return convertView;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}
